package top.drinyeit.weilong.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import org.w3c.dom.Text;

import java.util.List;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.activity.CommodityDetailActivity;
import top.drinyeit.weilong.activity.CommodityDetailNewActivity;
import top.drinyeit.weilong.activity.StoreActivity;
import top.drinyeit.weilong.model.CategoryStoreBean;

/**
 * Created by wu on 2018/1/18.
 */

public class ShopSearchResultAdapter extends RecyclerView.Adapter<ShopSearchResultAdapter.ViewHolder> {
    private List<CategoryStoreBean> list;
    private Context context;

    public ShopSearchResultAdapter(List<CategoryStoreBean> list) {
        this.list = list;
    }

    public void clear(){
        list.clear();
    }

    public void addData(List<CategoryStoreBean> list){
        this.list.addAll(list);
    }

    public List<CategoryStoreBean> getData(){
        return list;
    }

    public void setData(List<CategoryStoreBean> list){
        this.list = list;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView tv_store_name, tv_info, tv_go_store;
        public ImageView iv_head, iv_top0, iv_top1, iv_top2;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_store_name = itemView.findViewById(R.id.tv_store_name);
            tv_info = itemView.findViewById(R.id.tv_info);
            tv_go_store = itemView.findViewById(R.id.tv_go_store);
            iv_head = itemView.findViewById(R.id.iv_head);
            iv_top0 = itemView.findViewById(R.id.iv_top0);
            iv_top1 = itemView.findViewById(R.id.iv_top1);
            iv_top2 = itemView.findViewById(R.id.iv_top2);


            tv_go_store.setOnClickListener(this);
            iv_top0.setOnClickListener(this);
            iv_top1.setOnClickListener(this);
            iv_top2.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Intent intent;
            switch (v.getId()){
                case R.id.tv_go_store:
                    intent = new Intent(context, StoreActivity.class);
                    intent.putExtra("store_id", list.get(getAdapterPosition()).getId());
                    intent.putExtra("store_name",list.get(getAdapterPosition()).getStore_name());
                    intent.putExtra("",list.get(getAdapterPosition()).getHead_photo_url());
                    context.startActivity(intent);
                    break;
                case R.id.iv_top0:
                    if (list.get(getAdapterPosition()).getTop3Commoditys().size() > 0) {
                        intent = new Intent(context, CommodityDetailNewActivity.class);
                        intent.putExtra("id",list.get(getAdapterPosition()).getTop3Commoditys().get(0).getId());
                        context.startActivity(intent);
                    }
                    break;
                case R.id.iv_top1:
                    if (list.get(getAdapterPosition()).getTop3Commoditys().size() > 1) {
                        intent = new Intent(context, CommodityDetailNewActivity.class);
                        intent.putExtra("id",list.get(getAdapterPosition()).getTop3Commoditys().get(1).getId());
                        context.startActivity(intent);
                    }
                    break;
                case R.id.iv_top2:
                    if (list.get(getAdapterPosition()).getTop3Commoditys().size() > 2) {
                        intent = new Intent(context, CommodityDetailNewActivity.class);
                        intent.putExtra("id",list.get(getAdapterPosition()).getTop3Commoditys().get(2).getId());
                        context.startActivity(intent);
                    }
                    break;
            }
        }
    }

    @Override
    public ShopSearchResultAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View v = LayoutInflater.from(context).inflate(R.layout.item_shop_search_result, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ShopSearchResultAdapter.ViewHolder holder, int position) {
        Glide.with(context).load(list.get(position).getHead_photo_url())
                .dontTransform()
                .placeholder(R.drawable.ic_def)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(holder.iv_head);
        holder.tv_store_name.setText(list.get(position).getStore_name());
        holder.tv_info.setText("销量" + list.get(position).getSale_volume() + "  共" + list.get(position).getCommodity_sum() + "件商品");
        if (list.get(position).getTop3Commoditys().size() > 0) {
            Glide.with(context).load(list.get(position).getTop3Commoditys().get(0).getCover_img_url())
                    .placeholder(R.drawable.ic_def)
                    .dontTransform()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(holder.iv_top0);
        }
        if (list.get(position).getTop3Commoditys().size() > 1) {
            Glide.with(context).load(list.get(position).getTop3Commoditys().get(1).getCover_img_url())
                    .dontTransform()
                    .placeholder(R.drawable.ic_def)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(holder.iv_top1);
        }
        if (list.get(position).getTop3Commoditys().size() > 2) {
            Glide.with(context).load(list.get(position).getTop3Commoditys().get(2).getCover_img_url())
                    .dontTransform()
                    .placeholder(R.drawable.ic_def)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(holder.iv_top2);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }
}
